/*
 * CC3DemoMashUpLayer.m
 *
 * $Version: cocos3d 0.5.2 (f3df37821a3f) on 2011-03-13 $
 * Author: Bill Hollings
 * Copyright (c) 2010-2011 The Brenwill Workshop Ltd. All rights reserved.
 * http://www.brenwill.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * http://en.wikipedia.org/wiki/MIT_License
 * 
 * See header file CC3DemoMashUpLayer.h for full API documentation.
 */

#import "CC3DemoMashUpLayer.h"
#import "CC3DemoMashUpWorld.h"
#import "ccMacros.h"


/** Parameters for setting up the joystick and button controls */
#define kJoystickThumbFileName  @"JoystickThumb.png"
#define kJoystickSideLength     80.0
#define kJoystickPadding        300.0

@interface CC3DemoMashUpLayer (TemplateMethods)
- (void)addJoysticks;
- (void)addSwitchViewButton;
- (void)addInvasionButton;
- (void)positionLocationJoystick;
- (void)positionButtons;
@end


@implementation CC3DemoMashUpLayer

- (void)dealloc {
	directionJoystick = nil;		// retained as child
	locationJoystick = nil;			// retained as child
    [super dealloc];
}

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{

    CGPoint point=[touch locationInView:[touch view]];
	
		
	if (point.x<512)
	{
		NSLog(@"left");
		[(CC3DemoMashUpWorld *)self.cc3World CameraTurnLeft];
	}
	else {
		NSLog(@"right");
		
		[(CC3DemoMashUpWorld *)self.cc3World CameraTurnRight];
	}

	
}

/**
 * Returns the contained CC3World, cast into the appropriate type.
 * This is a convenience method to perform automatic casting.
 */
- (CC3DemoMashUpWorld*)pushBallWorld {
	return (CC3DemoMashUpWorld*) cc3World;
}

- (void)initializeControls {
	[self addJoysticks];i=0;
	self.isTouchEnabled = YES;		// Enable touch event handling for 3D object picking
}

/** Creates the two joysticks that control the 3D camera direction and location. */
- (void)addJoysticks {
	CCSprite* jsThumb;

    // Joystick direction.
	jsThumb = [CCSprite spriteWithFile:kJoystickThumbFileName];
	directionJoystick = [Joystick joystickWithThumb:jsThumb
											andSize:CGSizeMake(kJoystickSideLength, kJoystickSideLength)];
	directionJoystick.position = ccp(kJoystickPadding, kJoystickPadding);
	[self addChild:directionJoystick];

    // Joystick location.
	jsThumb = [CCSprite spriteWithFile:kJoystickThumbFileName];
    locationJoystick = [Joystick joystickWithThumb:jsThumb
										   andSize:CGSizeMake(kJoystickSideLength, kJoystickSideLength)];
	[self positionLocationJoystick];
	[self addChild:locationJoystick];
	
	
	
}

/**
 * Positions the right-side location joystick at the right of the layer.
 * This is called at initialization, and anytime the content size of the layer changes
 * to keep the joystick in the correct location within the new layer dimensions.
 */
- (void)positionLocationJoystick {
	locationJoystick.position = ccp(self.contentSize.width - kJoystickSideLength - kJoystickPadding, kJoystickPadding);
}

#pragma mark Updating

/**
 
 * Updates the player (camera) direction and location from the joystick controls
 * and then updates the 3D world.
 */
- (void)update:(ccTime)dt {
	
	// Update the player direction and position in the world from the joystick velocities
	self.pushBallWorld.playerDirectionControl = directionJoystick.velocity;
	self.pushBallWorld.playerLocationControl = locationJoystick.velocity;
    [self.pushBallWorld CameraMove];
	[super update:dt];
}

/**
 * Called automatically when the contentSize has changed.
 * Move the location joystick to keep it in the bottom right corner of this layer
 * and the switch view button to keep it centered between the two joysticks.
 */
- (void)didUpdateContentSizeFrom:(CGSize)oldSize {
	[super didUpdateContentSizeFrom: oldSize];
	[self positionLocationJoystick];
}

@end
